home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8671 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: newsserver.amsinc.com!usenet
  2. From: sherborn@mail.amsinc.com (Steve Herborn)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: need for function prototypes
  5. Date: Tue, 05 Mar 1996 19:10:44 GMT
  6. Organization: American Management Systems
  7. Message-ID: <4hi462$3bf@ams.amsinc.com>
  8. References: <4gutho$o1a@mn5.swip.net> <4hgbo0$2fj@s02.pavilion.co.uk>
  9. NNTP-Posting-Host: victor.amsinc.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. AJRobb@pavilion.co.uk (Andy J Robb) wrote:
  13.  
  14. >chris.rossall@mailbox.swipnet.se (Chris Rossall) wrote:
  15.  
  16. >>Hello I am having trouble understanding why I should use function
  17. >>prototypes. I mean,if the function is defined before it is used,the
  18. >>compiler should have enough information about the parameters to
  19. >>produce correct code anyway.
  20.  
  21. >>-Chris 
  22.  
  23. >-----BEGIN PGP SIGNED MESSAGE-----
  24.  
  25. >True, a function definition before a function call will serve as its
  26. >own declaration especially if it is restricted to file scope (static).
  27. >A lot of programs are written top-down.  This leaves the function
  28. >definition after the function call.  To get around this, insert a
  29. >function declaration before the function call.
  30.  
  31. >Function prototypes come in to their own when defining functions with
  32. >external linkage.  The declaration of these functions can then be
  33. >included in other files where the function is called.  Also, including
  34. >the header file in the function source file allows the declaration to
  35. >be checked against the definition.
  36.  
  37. >Regards,
  38. >Andy Robb.
  39.  
  40. >-----BEGIN PGP SIGNATURE-----
  41. >Version: 2.6.2i
  42.  
  43. >iQCVAwUBMTuyNpPl4P16x9sNAQFVIQQAvrKwK4vAjiGNYlP9olcWw6HkcJotYOhR
  44. >PDtvkY5d6ZWKCiv/7zrpI3Z/rAh5ThbSqzSQxdu7pM+rwQJf98D+lTXv+pD4m1Yr
  45. >zgr2+b88j5ttawySeQuzfOOCItb/jK5nSFMHQ1UO2o2Y8o3ELrCgG06tMAcmPaKW
  46. >DCwdsVWMpIw=
  47. >=dAKB
  48. >-----END PGP SIGNATURE-----
  49.  
  50. >-----BEGIN PGP PUBLIC KEY BLOCK-----
  51. >Version: 2.6.2i
  52.  
  53. >mQCNAy/MpRwAAAEEAOt6uBYqT8yv9EmqNhK8m6v+bYi8QjnGW3Bo6iU1gsMj5pa6
  54. >MHgq99c8deADbE3cbJ6uZS9v5pZE3WCf6HCQjlB5iULA5RZzMdAumd/WUzuL9UT3
  55. >B44D9EqqFIL79FlYb56v4oKFqFp1/J2bIpYUwnUvabGzGjdLrpPl4P16x9sNAAUR
  56. >tCNBbmR5IEogUm9iYiA8QUpSb2JiQHBhdmlsaW9uLmNvLnVrPrQhQW5keSBSb2Ji
  57. >IDxBSlJvYmJAcGF2aWxpb24uY28udWs+
  58. >=/wVD
  59. >-----END PGP PUBLIC KEY BLOCK-----
  60.  
  61. The major advantage is that the data types of a function's arguments
  62. are clearly specified at the begining of a program.  A common error in
  63. K&R programs was to call a function using the wrong data type for an
  64. argument; int, for example instead of long.  This could lead to
  65. program failure that was difficult to debug, because there was no
  66. warning from the compiler.  When a prototype is used, however, the
  67. compiler knows what data types to expect as arguments for the function
  68. and is always able to flag a mismatch as an error.  Also, as in
  69. declaring variables the prototype clarifies for the programmer (or
  70. anyone else) looking at the source listing what each function is and
  71. what its arguments should be.
  72.  
  73. The ANSI standard introduced type void for functions that dodn't
  74. return anything.  Previously, a function that didn't return anything
  75. was considered to be type int by default, an inconsistent and
  76. potenially confusing approach.
  77.  
  78. ANSI also introduced void as meaning that the function takes no
  79. arguments, thus removing another source of confusion.
  80.  
  81. Steve Herborn
  82. American Management Systems Inc
  83.  
  84. My opinions are my opinions.  However, the above is quoted from Robert
  85. Lafore's C Programming Book.
  86.  
  87.